package com.xiezc.util

import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.internal.Version
import okhttp3.internal.http.HttpHeaders
import okhttp3.internal.http.RealResponseBody
import okio.GzipSource
import okio.Okio

/**
 * @author wb-xzc291800
 * @date 2017/11/01 16:20
 */
class HeaderInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain?): Response {
        val userRequest = chain!!.request()
        val requestBuilder = userRequest.newBuilder()
        val accept = userRequest.header("Accept")
        if (accept != null) {
            requestBuilder.addHeader("Accept", "*/*")
        }
        val acceptLanguage = userRequest.header("Accept-Language")
        if (acceptLanguage != null) {
            requestBuilder.addHeader("Accept-Language", "zh-CN,zh;q=0.8")
        }
        val userAgent = userRequest.header("User-Agent")
        if (userAgent != null) {
            requestBuilder.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")
        }
        val networkResponse = chain.proceed(requestBuilder.build())
        val responseBuilder = networkResponse.newBuilder()
                .request(userRequest)
        return responseBuilder.build()
    }


}